/*
 * Group_DBImpl.java
 *
 * Created on February 12, 2007, 11:27 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.asaga.tools;

import java.sql.*;

/**
 *
 * @author jeremys
 */
public class Group_DBImpl implements Group{
    private int groupId;
    private String groupName;
    private String groupDesc;
    private String dateCreated;
    private String groupValidate;
    private String groupStatus;
    private String groupCreator;
    
    private DBPool connectionPool;
    private Connection conn;
    
    /** Creates a new instance of Group_DBImpl */
    public Group_DBImpl() {
        connectionPool = DBPool.getInstance();
        conn = connectionPool.getConnection();
        this.groupName="Larry";
        this.groupDesc="Curly";
        this.groupId=5;
    }
    
    public Group_DBImpl(int gId,Connection cn){
        connectionPool = DBPool.getInstance();
        conn = connectionPool.getConnection();
        String query = "select * from autismgroup where " +
                " groupID='"+gId+"'";
        //String query = "select * from autismgroup where groupID='3'";
        ResultSet rs = null;
        try{
           java.sql.Statement statement = conn.createStatement();
           rs = statement.executeQuery(query);
           if(rs.next()){
                this.groupName=rs.getString("name");
                this.dateCreated=rs.getString("dateCreated");
                this.groupDesc=rs.getString("description");
                this.groupStatus=rs.getString("status_2");
                this.groupValidate=rs.getString("validate");
                this.groupId=gId;
           }
        }catch(SQLException ex){
            // do something here
            this.groupName="Larry error";
            this.groupDesc = ex.toString();
            this.groupId = -1;
        }
    }

    public boolean createGroup(Connection cn) {
        
        if(this.groupId>=0){return false;}
        
        String insertQuery ="INSERT INTO autismgroup"
                +" (name,description,validate,status_2) VALUES "
                + "('"+this.groupName+"','"+this.groupDesc+"','"
                + this.groupValidate+"','"+this.groupStatus+"')";
        try{
            java.sql.Statement statement = conn.createStatement();
            int rowCount = statement.executeUpdate(insertQuery);
        }catch(SQLException ex){
            return false;
        }        
        return true;
    }
    
    public boolean createGroup(String gName,String gDesc,String gCreator,
                                Connection cn) {
        if(this.groupId>=0){return false;}
        
        String insertQuery ="INSERT INTO autismgroup"
                +" (name,description,validate,status_2) VALUES "
                + "('"+gName+"','"+gDesc+"','"
                + this.groupValidate+"','"+this.groupStatus+"')";
        try{
            java.sql.Statement statement = conn.createStatement();
            int rowCount = statement.executeUpdate(insertQuery);
        }catch(SQLException ex){
            return false;
        }        
        return true;

    }

    public boolean deleteGroup(int gId,Connection cn) {
        String insertQuery ="DELETE FROM autismgroup " +
                "WHERE groupID='"+gId+"'";
        try{
            java.sql.Statement statement = conn.createStatement();
            int rowCount = statement.executeUpdate(insertQuery);
        }catch(SQLException ex){
            return false;
        }        
        return true;
    }

    public boolean addToGroup(int gId,int pId,Connection cn) {
        String insertQuery ="INSERT INTO person_has_autismgroup"
                +" (autismgroup_groupID,person_personID) VALUES "
                + "('"+gId+"','"+pId+"')";
        try{
            java.sql.Statement statement = conn.createStatement();
            int rowCount = statement.executeUpdate(insertQuery);
        }catch(SQLException ex){
            return false;
        }        
        return true;
    }

    public boolean removeFromGroup(int gId,int pId,Connection cn) {
        String insertQuery ="DELETE FROM person_has_autismgroup WHERE " +
                " person_personID='"+pId+"' AND autismgroup_groupID='"+gId+"'";
        try{
            java.sql.Statement statement = conn.createStatement();
            int rowCount = statement.executeUpdate(insertQuery);
        }catch(SQLException ex){
            return false;
        }        
        return true;
    }
    
    public boolean updateGroup(Connection cn){
        //try{
            
        //}catch(SQLException ex){
        
        //}    
        return false;
    }
    
    /* The Getters */
    public int getGroupId(){
        return this.groupId;
    }
    public String getGroupDateCreated(){
        return this.dateCreated;
    }
    public String getGroupDesc(){
        return this.groupDesc;
    }
    public String getGroupName(){
        return this.groupName;
    }
    public String getGroupStatus(){
        return this.groupStatus;
    }
    public String getGroupValidate(){
        return this.groupValidate;
    }
    public String getGroupCreator(){
        return this.groupCreator;
    }
    /* The Setters */
    public void setGroupId(int gId,Connection cn){
        String query = "select * from autismgroup ag where" +
                "ag.groupID='"+gId+"'";
        ResultSet rs = null;
        try{
           java.sql.Statement statement = cn.createStatement();
           rs = statement.executeQuery(query);
           if(rs.next()){
                this.groupName=rs.getString("name");
                this.dateCreated=rs.getString("dateCreated");
                this.groupDesc=rs.getString("description");
                this.groupStatus=rs.getString("status_2");
                this.groupValidate=rs.getString("validate");
                this.groupId=gId;
           }
        }catch(SQLException ex){
            // do something here
        }
    }
    public void setGroupName(String gName){this.groupName=gName;}
    public void setGroupDesc(String gDesc){this.groupDesc=gDesc;}
    public void setGroupCreator(String gCreator){this.groupCreator=gCreator;}
    
    public String toString(){
        return this.groupName+","+this.groupDesc+","+this.groupId;
    }
}
